import express, { Request, Response, NextFunction } from 'express'
import { formatResponse } from '../utils/tool'
import { addBlogService, queryBlogByIdService, queryBlogsByConditonService, updateBlogService, deleteBlogService } from '../service/blogService'
const router = express.Router()

// 新增文章
router.post('/', async (req, res, next) => {
  await addBlogService(req.body)
  res.send(formatResponse(0, '新增成功', null))
})

// id查询
router.get('/:id', async (req, res, next) => {
  const blog = await queryBlogByIdService(req.params.id)
  res.send(formatResponse(0, '', blog))
})

// 分页查询
router.get('/', async (req, res, next) => {
  const blogs = await queryBlogsByConditonService(req.query)
  res.send(formatResponse(0, '', blogs))
})

// 修改
router.put('/:id', async (req, res, next) => {
  await updateBlogService(+req.params.id, req.body)
  res.send(formatResponse(0, '修改成功', null))
})

// 删除
router.delete('/:id', async (req, res, next) => {
  await deleteBlogService(+req.params.id)
  res.send(formatResponse(0, '删除成功', null))
})

export default router